#pragma once

#ifdef MSFACEDETECTOR_EXPORTS
#define MSFACEDETECTOR_API __declspec(dllexport)
#else
#define MSFACEDETECTOR_API __declspec(dllimport)
#endif

#include <windows.h>
#include <vector>
#include "opencv2/myopencv.hpp"

using namespace std;

#define LX 30
#define LY 45
#define RX 100
#define RY 45
#define WIDTH 130
#define HEIGHT 150
#define DIST(lx, ly, rx, ry) sqrt((lx - rx) * (lx - rx) + (ly - ry) * (ly - ry) + 0.0)
#define ANGLE(lx, ly, rx, ry) atan(1.0 * (ry - ly) / (rx - lx))

// Face Component
struct FaceComp
{
	RECT FaceBound;
	bool IsEyeDetected;
	POINT LeftEye;
	POINT RightEye;
};

inline MSFACEDETECTOR_API FaceComp MakeFaceComp( RECT & faceBound )
{
	FaceComp comp;
	comp.FaceBound.left = faceBound.left;
	comp.FaceBound.right = faceBound.right;
	comp.FaceBound.top = faceBound.top;
	comp.FaceBound.bottom = faceBound.bottom;

	comp.IsEyeDetected = false;

	return comp;
}

inline MSFACEDETECTOR_API FaceComp MakeFaceComp( RECT & faceBound, POINT & leftEye, POINT & rightEye )
{
	FaceComp comp;
	comp.FaceBound.left = faceBound.left;
	comp.FaceBound.right = faceBound.right;
	comp.FaceBound.top = faceBound.top;
	comp.FaceBound.bottom = faceBound.bottom;

	comp.IsEyeDetected = true;

	comp.LeftEye.x = leftEye.x;
	comp.LeftEye.y = leftEye.y;
	comp.RightEye.x = rightEye.x;
	comp.RightEye.y = rightEye.y;

	return comp;
}

class CFaceDetectorDLL;
class CComponentDetectorDLL;

class MSFACEDETECTOR_API CFaceDetector
{
private:
	unsigned char* CreateBuffer( IplImage* img );
	void ReleaseBuffer( unsigned char* & buffer );
public:
	CFaceDetector(void);
	~CFaceDetector(void);

	HRESULT Process( IplImage* img, vector<FaceComp> & result );
	vector<IplImage*> Crop( IplImage* img, const vector<FaceComp> & comps );
private:
	CFaceDetectorDLL* m_faceDetector;
	CComponentDetectorDLL* m_eyeDetector;
};
